package com.voja.mrvica2.rss;

import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import com.voja.mrvica2.App;
import com.voja.mrvica2.Entry;

import android.app.Activity;
import android.os.AsyncTask;

public class FeedTask extends AsyncTask<Void, Void, Void> {
    private String   url = "";
    private Activity activity;
    private String beforeMethod    = "";
    private String completedMethod = "";
    private Exception exception;
    
    
    public FeedTask(Activity activity, String url) {
        this.activity = activity;
        this.url      = url;
    }
    
    public FeedTask(Activity activity, String url, String before, String completed) {
        this.activity = activity;
        this.url      = url;
        this.beforeMethod = before;
        this.completedMethod = completed;
    }
    
    URL              feedURL;
    SAXParserFactory factory;
    SAXParser        parser;
    XMLReader        reader;
    XMLHandler       handler;

    @Override
    protected Void doInBackground(Void... arg0) {
        if (url.length() > 0) {
            try {
                feedURL = new URL(url);
                factory = SAXParserFactory.newInstance();
                parser  = factory.newSAXParser();
                reader  = parser.getXMLReader();
                handler = new XMLHandler();
                
                callActivityMethod(beforeMethod);
                
                reader.setContentHandler(handler);
                reader.parse(new InputSource(feedURL.openStream()));
                
            } catch (Exception e) {
                exception = e;
            }
        }
        
        return null;
    }
    
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        
        App.setContext(this.activity);
        if (exception == null) {
            try {
                ArrayList<Entry> data = handler.getEntries();
                callActivityMethod(completedMethod, data);
            } catch (Exception e) {
                App.error(e.getMessage());
            }
        } else {
            App.error(exception.getMessage());
        }
    }
    
    protected void callActivityMethod(String method) {
        callActivityMethod(method, null);
    }
    
    protected void callActivityMethod(String method, ArrayList<Entry> data) {
        if (method == "" || method == null) {
            return;
        }
        
        try {
            Class<?> klas = activity.getClass();
            
            if (data != null) {
                klas.getMethod(method, ArrayList.class)
                    .invoke(activity, data);
            } else {
                klas.getMethod(method)
                    .invoke(activity);
            }
        } catch (Exception e) {
            App.setContext(this.activity);
            App.error(e.getMessage());
        }
    }
}
